﻿using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Shapes;
using AzukiMap.Layer;
using AzukiMap.Map;
using AzukiMap.MapSource;
using AzukiMap.Renderer;
using AzukiMap.Request;
using AzukiMap.Tools;
using AzukiMap.Tools.Logger;
using GeoAPI.Geometries;
using GisSharpBlog.NetTopologySuite.Geometries;
using GisSharpBlog.NetTopologySuite.IO;
using Geometry=GisSharpBlog.NetTopologySuite.Geometries.Geometry;

namespace AzukiSamples
{
    public partial class MainPage : UserControl
    {
        private MapControl _mapControl;

        public MainPage()
        {
            InitializeComponent();
            MapInit();
            Logger.LogLevel = LogLevel.Warn;
        }

        private void MapInit()
        {
            _mapControl = new MapControl(MapCanvas)
                              {
                                  //Srid = 2444,      // Japan Plane Rectangular CS II
                                  //Srid = 2451,      // Japan Plane Rectangular CS IX
                                  Srid = 4326,    // WGS84
                              };

            //_mapControl.MoveToExtents(0,0,100000,100000);   // Japan Place Rectangular CS II
            //_mapControl.MoveToExtents(0,0,100000,100000);   // Japan Place Rectangular CS IX
            //_mapControl.MoveToExtents(139, 36, 142, 38);    // WGS84
            _mapControl.MoveToExtents(139.520, 34.0799, 139.532, 34.0921);    // WGS84 miyake
            //SetGSI();
            SetBingMap();
            SetMiyakeRoad();
            SetRelief();
            SetMadFlow();
        }

        private void SetBingMap()
        {
            var bingSource = new BingSource("Virtual Earth");
            bingSource.TileType = BingLayerType.ROAD;
            //veSource.TileType = VirtualEarthLayerType.HYBRID;
            //bingSource.TileType = BingLayerType.AERIAL;
            var veLayer = new BingLayer(bingSource, "Bing");
            veLayer.Visibility = Visibility.Visible;
            veLayer.Opacity = 0.6;
            _mapControl.AddLayerBottom(veLayer);
        }

        private void SetGSI()
        {
            var layer = new GsiLayer("Denshi Kokudo")
                            {
                                Visibility = Visibility.Visible,
                                Opacity = 0.6
                            };
            _mapControl.AddLayerBottom(layer);
        }

        private void SetRelief()
        {
            var source = new WMSSource("Relief", 4326)
                             {
                                 RequestUri = "http://anko.azukimap.jp/cgi-bin/relief_4326",
                                 MaxWidth = 2000,
                                 MaxHeight = 2000,
                                 MaxScale = 500000,
                                 Version = WMSVersion.V1_1_1,
                                 Format = RasterFileSourceFormat.JPEG,
                                 Bgcolor = new Color {R = 180, G = 180, B = 180}
                             };
            source.Layers.Add("relief_4326");

            var layer = new WMSLayer(source, "Relief")
                            {
                                Opacity = 0.25
                            };
            _mapControl.AddLayerTop(layer);
        }

        private void SetWMSAero()
        {
            var source = new WMSSource("Mlit ortho", 4612)
                             {
                                 RequestUri = "http://orthophoto.mlit.go.jp:8888/wms/service/wmsRasterTileMap",
                                 MaxHeight = 910,
                                 MaxWidth = 910,
                                 MaxScale = 50000,
                                 Version = WMSVersion.V1_3_0,
                                 Format = RasterFileSourceFormat.JPEG,
                                 BBoxLatLonOrder = LatLonOrder.LAT_LON
                             };

            source.Layers.Add("ORTHO");

            var layer = new WMSLayer(source, "Aero 70's (WMS)")
                            {
                                MaxScale = source.MaxScale,
                                Visibility = Visibility.Collapsed
                            };

            _mapControl.AddLayer(layer);
        }

        private void SetMiyakeRoad()
        {
            var source = new PostGisSource(VectorSourceTypes.MultiLineString)
                             {
                                 Name = "road",
                                 Srid = 4301,
                                 DbName = "miyake",
                                 GeometryColumn = "the_geom",
                                 TableName = "road_bl",
                                 GetColumnDataUri = "http://azukimap.jp/amdev/getGeomDataAttr.php?"
                             };

            var layer = new PostGisLayer(source, "road");
            /*
            layer.Labels.Add("大分類");
            layer.Labels.Add("小分類");
            layer.ElementMouseLeftButtonDown += new System.Windows.Input.MouseButtonEventHandler(layer_ElementMouseLeftButtonDown);
            layer.ElementMouseEnter += new System.Windows.Input.MouseEventHandler(layer_ElementMouseEnter);
            layer.ElementMouseLeave += new System.Windows.Input.MouseEventHandler(layer_ElementMouseLeave);
             */

            _mapControl.AddLayerTop(layer);
        }

        private void SetMadFlow()
        {
            var source = new PostGisSource(VectorSourceTypes.MultiPolygon)
                             {
                                 Name = "mad flow",
                                 Srid = 4326,
                                 DbName = "miyake",
                                 GeometryColumn = "the_geom",
                                 TableName = "miyake_madflowp_bl",
                                 GetColumnDataUri = "http://azukimap.jp/amdev/getGeomDataAttr.php?",
                             };
            var layer = new PostGisLayer
                            {
                                PgSource = source,
                                Name = "mad flow",
                            };

            _mapControl.AddLayerTop(layer);
        }


        private void layer_ElementMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            var id = (int) ((FrameworkElement) sender).Resources["id"];
            //MessageBox.Show(id.ToString());
        }

        private void layer_ElementMouseLeave(object sender, MouseEventArgs e)
        {
            ((Shape) sender).Stroke = new SolidColorBrush(Colors.Black);
            ((Shape) sender).Fill.Opacity = 1;
        }

        private void layer_ElementMouseEnter(object sender, MouseEventArgs e)
        {
            ((Shape) sender).Stroke = new SolidColorBrush(Colors.Yellow);
            ((Shape) sender).Fill.Opacity = 0.5;
        }

        private void SetTestRect()
        {
            var geomUtil = new GeometryUtil();
            IPolygon rect4326 = geomUtil.CreateExtentPolygon(139, 36, 140, 37);
            rect4326.SRID = 4326;
            var d4326 = new LocalVectorData
                            {
                                Geometry = (Geometry) rect4326
                            };
            var list4326 = new List<LocalVectorData> {d4326};
            var source4326 = new LocalVectorSource(VectorSourceTypes.Polygon)
                                 {
                                     Srid = 4326,
                                     Name = "rect 4326",
                                     Items = list4326
                                 };

            var layer4326 = new LocalVectorLayer
                                {
                                    Name = "layer4326",
                                    LocalVectorSource = source4326,
                                };

            _mapControl.AddLayerTop(layer4326);

            // 140, 36, 141, 37 by web mercator.
            IPolygon rect900913 = geomUtil.CreateExtentPolygon(15584728.71, 4300621.37, 15696048.20, 4439106.79);
            rect900913.SRID = 900913;
            var d900913 = new LocalVectorData
                              {
                                  Geometry = (Geometry) rect900913
                              };
            var list900913 = new List<LocalVectorData> {d900913};
            var source900913 = new LocalVectorSource(VectorSourceTypes.Polygon)
                                   {
                                       Srid = 900913,
                                       Name = "rect 900913",
                                       Items = list900913
                                   };

            var layer900913 = new LocalVectorLayer
                                  {
                                      Name = "layer900913",
                                      LocalVectorSource = source900913,
                                  };

            _mapControl.AddLayerTop(layer900913);

            // 140, 36, 141, 37 by web mercator.
            IPolygon rect3785 = geomUtil.CreateExtentPolygon(15584728.71, 4300621.37, 15696048.20, 4439106.79);
            rect3785.SRID = 3785;
            var d3785 = new LocalVectorData
                            {
                                Geometry = (Geometry) rect3785
                            };
            var list3785 = new List<LocalVectorData> {d3785};
            var source3785 = new LocalVectorSource(VectorSourceTypes.Polygon)
                                 {
                                     Srid = 3785,
                                     Name = "rect 3785",
                                     Items = list3785
                                 };

            var layer3785 = new LocalVectorLayer
                                {
                                    Name = "layer3785",
                                    LocalVectorSource = source3785,
                                };

            _mapControl.AddLayerTop(layer3785);
        }

    }
}